import { ObjectDirective } from 'vue'
import usePermissionStore from '@/store/modules/permission'
import { doAsyncRoutes } from '@/setting'

/**
 * @el：获取绑定的dom
 * @binding.value：通过指令绑定之后传过来的参数
 * 具体的权限判断代码我放到hasPermission()函数中
 */

export const permission: ObjectDirective = {
  mounted(el: HTMLButtonElement, binding) {
    if (binding.value == undefined) return
    // 关闭异步路由不控制按钮权限
    if (doAsyncRoutes == false) return

    // 将获取到的值传到权限判断函数中，如果函数返回false，删除dom----before
    const { resourcePermission } = usePermissionStore()
    const code = binding.value
    if (resourcePermission.includes(code) == false) {
      el.remove()
    }
    // end
  },
}
